package fons.navigator.views.quadrant;

import java.awt.Font;
import java.awt.geom.Rectangle2D;
import java.util.List;
import java.util.Vector;

import edu.umd.cs.piccolo.PNode;
import edu.umd.cs.piccolo.event.PBasicInputEventHandler;
import edu.umd.cs.piccolo.event.PInputEvent;
import edu.umd.cs.piccolo.nodes.PPath;
import edu.umd.cs.piccolo.nodes.PText;
import fons.model.ontology.Colors;
import fons.navigator.util.piccolo.Legend.ItemProducer;

public class SpiralLegendItemProducer implements ItemProducer {

	private final VisualSpiral visualSpiral;
	private final static Font LEGEND_FONT = new Font("Arial", Font.BOLD, 16);
	
	public SpiralLegendItemProducer (VisualSpiral visualSpiral) {
		this.visualSpiral = visualSpiral;
	}
	
	@Override
	public PNode createHeader() {
		PText text = new PText("Legende");
		text.setFont(LEGEND_FONT);
		
		PNode textNode = new PNode();
		textNode.setBounds(5, 5, 100, 10);
		textNode.addChild(text);
		text.offset(10, 4);
		
		return textNode;
	}

	@Override
	public Iterable<PNode> createItems() {
		List<PNode> nodes = new Vector<PNode>();

		double x = 0;
		double y = 0;
		
		for (final Spiral spiral: visualSpiral.getSpirals()) {			
			PNode legendNode = new PNode();

			PPath colorRect = new PPath(new Rectangle2D.Double(x, y, 18, 14));
			colorRect.setPaint(Colors.getColorFor(spiral.getTargetDimension(), "node"));
				
			PText text = new PText(spiral.getTargetDimension().getName());
			text.setFont(new Font("Arial", Font.BOLD, 11));
			text.addInputEventListener(new PBasicInputEventHandler() {
				@Override
				public void mouseClicked(PInputEvent event) {
					if (event.getClickCount() == 2) {
						visualSpiral.getCamera().animateViewToCenterBounds(spiral.getBounds(), true, 1000);
					}
				}
			});
			text.setBounds(x+18, y+2, 0, 0);
			
			legendNode.addChild(colorRect);
			legendNode.addChild(text);
			
			nodes.add(legendNode);
		}
		
		return nodes;
	}

}
